package com.czq.controllor;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czq.common.Result;
import com.czq.pojo.CateGory;
import com.czq.pojo.Setmeal;
import com.czq.pojo.SetmealDish;
import com.czq.pojo.SetmealDto;
import com.czq.service.CategoryService;
import com.czq.service.SetmealDishService;
import com.czq.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequestMapping("/setmeal")
@RestController
@Transactional
public class SetmealControllor {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @CacheEvict(value ="setmealCache",allEntries = true)
    public Result<String> addsetmeal(@RequestBody SetmealDto setmealDto) {

        setmealService.save(setmealDto);
        Long categoryId = setmealDto.getCategoryId();
        LambdaQueryWrapper<Setmeal> query = new LambdaQueryWrapper<>();
        query.eq(Setmeal::getCategoryId, categoryId);
        Setmeal one = setmealService.getOne(query);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(one.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
        return Result.success("添加套餐成功");
    }

    @GetMapping("/page")
    public Result<Page> pageSetmeal(Integer page, Integer pageSize, String name) {
        Page p = new Page(page, pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<Setmeal>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        setmealService.page(p, queryWrapper);
        Page<SetmealDto> s = new Page<SetmealDto>();
        BeanUtils.copyProperties(p, s, "records");

        List<Setmeal> records = p.getRecords();
        List<SetmealDto> SetmealDtopage = new ArrayList<SetmealDto>();
        for (Setmeal dto : records) {
            Long categoryId = dto.getCategoryId();
            LambdaQueryWrapper<CateGory> q = new LambdaQueryWrapper<CateGory>();
            q.eq(CateGory::getId, categoryId);
            CateGory one = categoryService.getOne(q);
            String CategoryName = one.getName();
            SetmealDto setmealDto = new SetmealDto();
            setmealDto.setCategoryName(CategoryName);
            BeanUtils.copyProperties(dto, setmealDto);
            SetmealDtopage.add(setmealDto);
        }
        s.setRecords(SetmealDtopage);


        return Result.success(s);
//return  Result.success(p);

    }

    @Transactional
    @DeleteMapping
    @CacheEvict(value ="setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {

        setmealService.delete(ids);
        return Result.success("删除成功");
    }

    @PostMapping("/status/{status}")
    @CacheEvict(value ="setmealCache",allEntries = true)
    public Result<String> status(@PathVariable Integer status, @RequestParam List<Long> ids) {

        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<Setmeal>();
        updateWrapper.set(Setmeal::getStatus, status).in(Setmeal::getId, ids);
        setmealService.update(updateWrapper);


//        List<Setmeal> setmeals = setmealService.listByIds(ids);.
//        for (Setmeal setmeal : setmeals) {
//            setmeal.setStatus(status);
//        }
//
//        setmealService.updateBatchById(setmeals);
        return Result.success("修改成功");
    }

    @GetMapping("/list")
//    @CachePut(value = "setmealCache" , key="#categoryId +'_'+#status")
    @Cacheable(value = "setmealCache" , key="#categoryId +'_'+#status")
    public Result<List<Setmeal>>  getsetmeal(@RequestParam Long categoryId,@RequestParam Integer status  ){

        LambdaQueryWrapper<Setmeal> setmealQuery = new LambdaQueryWrapper<Setmeal>();
        setmealQuery.eq(Setmeal::getStatus,status).eq(Setmeal::getCategoryId,categoryId);
        List<Setmeal> list = setmealService.list(setmealQuery);
        return  Result.success(list);
    }

}
